<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('基金名称')" prop="fundName">
            <el-input v-model="form.fundName" :placeholder="$t('基金名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="entryName">
            <el-input v-model="form.entryName" :placeholder="$t('项目名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('负责人')" prop="chargePerson">
            <el-input v-model="form.chargePerson" :placeholder="$t('负责人')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资时间')" prop="investmentTime">
            <DatePicker v-model="form.investmentTime" :placeholder="$t('投资时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资金额') + $t('金额单位')" prop="investmentAmount">
            <Money v-model="form.investmentAmount" unit="w" :placeholder="$t('投资金额')" @change="calcMultiple" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回款金额') + $t('金额单位')" prop="receivedAmount">
            <Money v-model="form.receivedAmount" unit="w" :placeholder="$t('回款金额')" @change="calcMultiple" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回报倍数')" prop="returnMultiple">
            <span slot="label">
              {{ $t('回报倍数') }}
              <el-tooltip :content="$t('(回款金额 / 投资金额)')" placement="top">
                <i class="el-icon-question" />
              </el-tooltip>
            </span>
            <Money v-model="form.returnMultiple" :placeholder="$t('回报倍数')" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('IRR')" prop="irr">
            <el-input v-model="form.irr" :placeholder="$t('IRR')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--      <FileTable :list.sync="form.fileList" data-type="bf_achievement" :business-id="gpId" />-->
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="bf:achievement" />
    </div>
  </div>
</template>

<script>
    import { saveBfHistoryAchievement, getBfHistoryAchievement } from '@/api/bf/achievement/bfHistoryAchievement'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                  fileList: []
                },
                // 项目id
                gpId: this.$route.query.gpId,
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  fundName: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  entryName: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                }
            }
        },
        mounted() {
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getBfHistoryAchievement(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 计算回报倍数
            calcMultiple() {
              if (this.form.receivedAmount !== undefined && this.form.investmentAmount !== undefined && this.form.investmentAmount !== 0) {
                this.$set(this.form, 'returnMultiple', this.form.receivedAmount / this.form.investmentAmount)
              }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.dealId,
                    gpId: this.$route.query.gpId,
                    fundName: undefined,
                    entryName: undefined,
                    chargePerson: undefined,
                    investmentTime: undefined,
                    investmentAmount: undefined,
                    receivedAmount: undefined,
                    returnMultiple: undefined,
                    irr: undefined,
                    remark: undefined,
                    fileList: [],
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveBfHistoryAchievement(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           }
        }
    }
</script>
